Pointers in C++

Introduction

C++ pointers are fundamental elements in the language that allow you to work with memory addresses and manipulate data directly in memory. Understanding pointers is crucial for many advanced C++ programming tasks, such as dynamic memory allocation, data structures, and low-level memory operations.

Declaring Pointers

Syntax

type* pointerName;

Where:

  • type is the data type the pointer points to.
  • pointerName is the name of the pointer variable.

Example:

int* intPointer;
double* doublePointer;

Initializing Pointers

Null Pointers

You can initialize a pointer to a null value using the nullptr keyword.

int* nullPointer = nullptr;

Pointer to an Existing Variable

To initialize a pointer with the address of an existing variable, you use the address-of operator &.

int x = 42;
int* pointerToX = &x;

Dereferencing Pointers

Dereferencing a pointer means accessing the value it points to. You use the dereference operator * for this purpose.

int value = *pointerToX; // value now contains 42

Pointer Arithmetic

C++ allows you to perform arithmetic operations on pointers.

  • Adding an integer n to a pointer advances it by n elements of its base type.
  • Subtracting an integer n from a pointer moves it back by n elements.
int arr[] = {10, 20, 30, 40, 50};
int* ptr = arr; // Points to the first element (arr[0])

int thirdElement = *(ptr + 2); // Access the third element (30)

Pointers and Arrays

In C++, pointers and arrays are closely related. An array name can be used as a pointer to its first element.

int numbers[] = {1, 2, 3, 4, 5};
int* ptr = numbers; // ptr points to numbers[0]

Dynamic Memory Allocation

C++ allows you to allocate memory dynamically using new and deallocate it using delete.

int* dynamicInt = new int; // Allocate memory for an integer
*dynamicInt = 123; // Assign a value
delete dynamicInt; // Deallocate memory

Pointer to Functions

Pointers can also point to functions, which is useful for implementing callbacks and dynamic function selection.

int add(int a, int b) {
    return a + b;
}

int (*funcPointer)(int, int) = add;
int result = funcPointer(2, 3); // result contains 5